//
//  Extensions.swift
//  Youtube2
//
//  Created by 劢克科技 on 2019/5/5.
//  Copyright © 2019 meckey. All rights reserved.
//

import UIKit

extension UIView {
    static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
        return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 1)
    }
}

let imageCache = NSCache<AnyObject, AnyObject>()

extension UIImageView {
    func downloadImage(urlString: String) {
        
        if let cacheImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage{
            self.image = cacheImage
            return
        }
        
        // 下载Thumb
        URLSession.shared.dataTask(with: URL(string: urlString)!) { (data, _, err) in
            
            // 处理错误
            if err != nil {
                print(err as Any)
                return
            }
            
            DispatchQueue.main.async {
                let cacheImage = UIImage(data: data!)
                imageCache.setObject(cacheImage!, forKey: urlString as AnyObject)
                self.image = cacheImage
            }
            }.resume()
    }
}
